package com.samsung.spen.sshare.networking.sendreceivefile;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.samsung.spen.sshare.R;
import com.samsung.spen.sshare.explorer.choosefiles.FilesSelectedActivity;
import com.samsung.spen.sshare.explorer.util.SConfig;
import com.samsung.spen.sshare.networking.networkservice.SendReceiveService;
import com.samsung.spen.sshare.networking.networkservice.SendingDataTask;

public class SendReceiveActivity extends Activity {

	private Button mSelectFiles, mSendListFiles;

	public static ArrayList<SubFileExtend> filesRequest = new ArrayList<SubFileExtend>();
	private static ArrayList<SubFileExtend> filesSending = new ArrayList<SubFileExtend>();
	private static FileAdapter adapterRequest;
	private static FileAdapter adapterSending;
	private ListView lvRequest;
	private ListView lvSending;
	private static final String TAG = "SENDRECEIVEACTIVITY";

	public static final int REQUEST = 0;
	public static final int SENDING = 1;

	private static String mIP = "";
	private static String mPort = "";

	public static Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case SendReceiveService.RECEIVE:
				String received = (String) msg.obj;
				Log.i(TAG, received);
				String[] tokens = received.split("%");
				if (tokens[0].toUpperCase().equals(SendReceiveService.DATALIST)) {
					Log.i(TAG, "LIST");
					String[] fileSets = tokens[1].split(";");
					for (String fileSet : fileSets) {
						String fileName = fileSet.split(",")[0];
						String fileSize = fileSet.split(",")[1];
						SubFileExtend file = new SubFileExtend(fileName,
								fileSize);
						adapterRequest.add(file);
						adapterRequest.notifyDataSetChanged();
					}
				} else if (tokens[0].toUpperCase().equals(
						SendReceiveService.INDEX)) {
					Log.i(TAG, "INDEX " + tokens[1]);
					new SendingDataTask(filesSending.get(Integer
							.parseInt(tokens[1]))).execute(mIP, mPort,
							SendReceiveService.DATA,
							filesSending.get(Integer.parseInt(tokens[1]))
									.getFile().getAbsolutePath());

				} else {
					Log.i(TAG, "NOTHING");
				}

				break;

			default:
				break;
			}

		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.network_activity_send_receive);

		Intent i = getIntent();

		String data = i.getStringExtra(SendReceiveService.DATALIST);
		if (data != null) {
			Message msgSendReceive = SendReceiveActivity.mHandler
					.obtainMessage();
			msgSendReceive.what = SendReceiveService.RECEIVE;
			msgSendReceive.obj = data;
			SendReceiveActivity.mHandler.sendMessage(msgSendReceive);
		}
		mIP = i.getStringExtra(SendReceiveService.IP);
		mPort = i.getStringExtra(SendReceiveService.PORT);

		Log.i(TAG, mIP + "");
		Log.i(TAG, mPort + "");

		mSelectFiles = (Button) findViewById(R.id.btnSelectFiles);
		mSendListFiles = (Button) findViewById(R.id.btnSendListFile);

		lvRequest = (ListView) findViewById(R.id.lvRequest);
		if (filesRequest.size() != 0) {
			filesRequest.removeAll(filesRequest);
		}
		adapterRequest = new FileAdapter(this, R.layout.network_lv_item_row,
				filesRequest, REQUEST);
		adapterRequest.setIP(mIP);
		adapterRequest.setPort(mPort);
		lvRequest.setAdapter(adapterRequest);

		lvSending = (ListView) findViewById(R.id.lvReceive);
		if (filesSending.size() != 0) {
			filesSending.removeAll(filesSending);
		}
		adapterSending = new FileAdapter(this, R.layout.network_lv_item_row,
				filesSending, SENDING);
		lvSending.setAdapter(adapterSending);

		mSelectFiles.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(SendReceiveActivity.this,
						FilesSelectedActivity.class);
				startActivityForResult(i, REQUEST_SELECT_FILE);

			}
		});

		mSendListFiles.setOnClickListener(new OnClickListener() {
			// image.jpg,100;imge2.exe,200
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String listFile = "";
				for (SubFileExtend file : filesSending) {
					String fileName = file.getFileName();
					String fileSize = file.getFileSize();
					listFile += fileName + "," + fileSize + ";";
				}
				Log.i(TAG, listFile);

				Log.i(TAG, mIP);
				Log.i(TAG, "giua");
				Log.i(TAG, mPort);
				new SendingDataTask().execute(mIP, mPort,
						SendReceiveService.DATALIST, listFile);
			}
		});

	}

	public static final int REQUEST_SELECT_FILE = 100;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == RESULT_OK && requestCode == REQUEST_SELECT_FILE){

			//if (data.hasExtra(SConfig.EXTRA_ACTION)) {

				String result = data.getStringExtra(SConfig.EXTRA_DATA);
				Log.e("Files ve ==>", result);

				filesSending = SubFileExtend.parseFiles(result);
				
				adapterSending=new FileAdapter(this, R.layout.network_lv_item_row, filesSending, SENDING);
				lvSending.setAdapter(adapterSending);
				//adapterSending.notifyDataSetChanged();
			}

		// //////////
	}
}
